/**
 * Created by WangMing on 15/12/9.
 */
define(["../../service",
  "../../utils/i18n",
  "../../widgets/breadcrumb_widget/breadcrumb_widget"
  ], function (api, i18n) {
  
  // 定义所有相关的vmodel
  var vmodel = avalon.define("search",function(vm){
   
    vm.keyword =  "";
    vm.$breadcrumb = {
        stateName:"search",
        guid: "",
        title: null // translation later in routers
      };
    vm.videosViewVisiable = true;

    vm.albumsViewVisiable = false;
    vm.livesViewVisiable = false;


    vm.videosSearchOptions ={
      params: {
        method: api.getVideos,    
        query:{
          queryString: [],
          order: null
        }
      }
    };
    vm.albumsSearchOptions = {
      params: {
        method: api.getAlbums,    
        query:{
          queryString: [],
          order: null
        }
      }
    }
    vm.livesSearchOptions={
      params: {
        method: api.getLives,    
        query:{
          queryString: [],
          order: null
        }
      }
    }

    vm.collectionsTypes = [
      {
        type: "videos",
        title: i18n.gettext("videos collections")
        
      },
       {
        type: "albums",
        title: i18n.gettext("albums collections")
        
      },
       {
        type: "lives",
        title: i18n.gettext("lives collections")
        
      }
    ];
     vm.currentPill = vm.collectionsTypes[0];
    vm.$skipArray = ["collectionsTypes","currentPill",
    "videosSearchOptions",
    "albumsSearchOptions",
    "livesSearchOptions",
    "breadcrumbWidget"
    ];
    
    vm.changeTypes = function(el){
      if(vm.currentPill != el){
        
        switch(el.type){
          case "videos":
            vm.videosViewVisiable = true;
            vm[vm.currentPill.type+"ViewVisiable"] = false;
            break;
          case "albums":
            vm.albumsViewVisiable = true;
            vm[vm.currentPill.type+"ViewVisiable"] = false;
            break;
          case "lives":
            vm.livesViewVisiable = true;
            vm[vm.currentPill.type+"ViewVisiable"] = false;
            break;
          default:
            break;
        }
        vm.currentPill = el;
      }
     


    }

  });

 
  


  return avalon.controller(function ($ctrl) {
    // 视图渲染后，意思是avalon.scan完成
    $ctrl.$onRendered = function () {
     
    };
    // 进入视图
    $ctrl.$onEnter = function (param, rs, rj) {
      if(param.keyword){
        vmodel.keyword = param.keyword;
       
        vmodel.$breadcrumb.subtitle = param.keyword; 
        

        _.each([vmodel.albumsSearchOptions,
          vmodel.albumsSearchOptions,
          vmodel.livesSearchOptions], function(t){
           _.merge(t.params, {
            query:{
              queryString:"keyword="+vmodel.keyword
            }
          });
        })
      }

     

    };
    // 对应的视图销毁前
    $ctrl.$onBeforeUnload = function () {

    };
    $ctrl.$vmodels = [vmodel];
  })
});